const BaseController = require('./base_controller.js');
const FosterRoomService = require('../service/foster_room_service.js');

class FosterRoomController extends BaseController {

  /**
   * 添加寄养房间
   */
  async addRoom() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      roomId: 'required|string|min:1|max:20|name=房间编号',
      roomName: 'required|string|min:1|max:50|name=房间名称',
      roomType: 'required|string|in:single,double,suite|name=房间类型',
      size: 'string|max:50|name=房间尺寸',
      capacity: 'required|int|min:1|name=容纳数量',
      price: 'required|int|min:1|name=价格',
      unit: 'string|max:20|name=价格单位',
      description: 'string|max:500|name=房间描述',
      amenities: 'array|name=房间设施',
      careInstructions: 'string|max:500|name=护理说明',
      storeId: 'required|string|name=门店ID',
    };

    let input = this.validateData(rules);

    try {
      const service = new FosterRoomService();
      const result = await service.addRoom(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 删除寄养房间
   */
  async deleteRoom() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      roomId: 'required|string|name=房间ID',
    };

    let input = this.validateData(rules);

    try {
      const service = new FosterRoomService();
      await service.deleteRoom(input.roomId);
      
      return this.success({ message: '房间删除成功' });
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 更新寄养房间
   */
  async updateRoom() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      roomId: 'required|string|name=房间ID',
      roomName: 'string|min:1|max:50|name=房间名称',
      roomType: 'string|in:single,double,suite|name=房间类型',
      size: 'string|max:50|name=房间尺寸',
      capacity: 'int|min:1|name=容纳数量',
      price: 'int|min:1|name=价格',
      unit: 'string|max:20|name=价格单位',
      description: 'string|max:500|name=房间描述',
      amenities: 'array|name=房间设施',
      careInstructions: 'string|max:500|name=护理说明',
      status: 'int|in:0,1|name=房间状态',
    };

    let input = this.validateData(rules);

    try {
      const service = new FosterRoomService();
      const result = await service.updateRoom(input.roomId, input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }

  /**
   * 获取房间列表
   */
  async getRoomList() {
    await this.isAdmin();

    // 数据校验
    let rules = {
      page: 'int|min:1|default=1|name=页码',
      size: 'int|min:1|max:50|default=20|name=每页数量',
      storeId: 'string|name=门店ID',
      roomType: 'string|name=房间类型',
      status: 'int|name=房间状态',
      search: 'string|max:50|name=搜索关键词',
    };

    let input = this.validateData(rules);

    try {
      const service = new FosterRoomService();
      const result = await service.getRoomList(input);
      
      return this.success(result);
    } catch (error) {
      return this.fail(error.message);
    }
  }
}

module.exports = FosterRoomController;
